/**
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

import { Pipe, PipeTransform } from '@angular/core';

import { NzSafeAny } from 'ng-zorro-antd/core/types';

@Pipe({
  name: 'nzEllipsis',
  standalone: true
})
export class NzEllipsisPipe implements PipeTransform {
  /**
 * 截取字符串，并添加后缀
 * @param value 要截取的字符串
 * @param length 截取的长度
 * @param suffix 后缀
 * @returns 截取后的字符串
 */
  transform(value: NzSafeAny, length?: number, suffix: string = ''): NzSafeAny {
    // 如果value不是字符串，直接返回
    if (typeof value !== 'string') {
      return value;
    }

    // 如果length未定义，则取value的长度
    const len = typeof length === 'undefined' ? value.length : length;

    // 如果value长度小于等于len，直接返回
    if (value.length <= len) {
      return value;
    }

    // 截取字符串，并添加后缀
    return value.substring(0, len) + suffix;
  }
}
